package ExamSystemClient.client;

import ExamSystemClient.model.ChoiceQuestion;
import ExamSystemClient.model.Student;

import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 学员系统
 *
 * @author Shuxian Huang
 */
public class StudentSystem {

    /**
     * 学生账户
     */
    private Student student;
    /**
     * 题库
     */
    private List<ChoiceQuestion> questions;

    public StudentSystem(Student student, List<ChoiceQuestion> questions) {
        this.student = student;
        this.questions = questions;
    }

    public Student getStudent() {
        return student;
    }

    public void studentOperate() throws IOException {
        while(true) {
            //1、管理员操作界面绘制
            System.out.println("  \n\n\t\t    学员系统");
            System.out.println("---------------------------------");
            System.out.print("  [1]用户模块");
            System.out.println("  [2]考试模块");
            System.out.println("  [0]退出系统");
            System.out.println("---------------------------------");
            System.out.println("请选择要进行的业务编号：");

            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    System.out.println("正在进入用户模块");
                    setAccount();
                    break;
                case 2:
                    System.out.println("正在进入考试模块");
                    exam();
                    break;
                case 0:
                    System.out.println("正在退出学员系统。。。");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    private void setAccount(){
        while(true) {
            System.out.println("  \n\n\t\t   用户模块");
            System.out.println("---------------------------------");
            System.out.print("  [1]修改密码");
            System.out.println("  [0]退出模块");
            System.out.println("---------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    modifyPassword();
                    break;
                case 0:
                    System.out.println("正在退出用户模块。。。");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    private void modifyPassword(){
        System.out.println("请输入新密码：");
        String p1 = ClientScanner.getScanner().next();
        System.out.println("请再次输入新密码：");
        String p2 = ClientScanner.getScanner().next();
        if (p1.equals(p2)){
            student.setPassword(p1);
            System.out.println("修改密码成功！");
        }else{
            System.out.println("两次输入的密码不一致！修改密码失败！");
        }
    }

    private void exam() throws IOException {
        while(true) {
            System.out.println("  \n\n\t\t  考试模块");
            System.out.println("---------------------------------");
            System.out.print("  [1]开始考试");
            System.out.println("  [2]导出成绩");
            System.out.print("  [3]查询成绩");
            System.out.println("  [0]退出系统");
            System.out.println("---------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    startExamination();
                    break;
                case 2:
                    exportGrades();
                    break;
                case 3:
                    searchGrades();
                    break;
                case 0:
                    System.out.println("正在退出考题管理模块。。。");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    private void startExamination(){
        System.out.println("开始考试");
        //1、从题库中随机选出5道题并记录得分
        //初始得分为100
        double grade = 100;
        int questionSize = questions.size();
        List<Integer> questionIndexs = new ArrayList<>();
        int selectNum = 0;
        while(selectNum < 5){
            //1.1 随机选不重复的一题
            int index = (int) (Math.random() * questionSize);
            if (questionIndexs.contains(index)){
                continue;
            }
            questionIndexs.add(index);
            //1.2 打印考题信息
            ChoiceQuestion q = questions.get(index);
            System.out.println("第" + (++ selectNum) + "题：");
            System.out.println(q.printQuestion());
            //1.3 请学生输入答案
            System.out.println("请输入回答选项：");
            String answer = ClientScanner.getScanner().next();
            System.out.println();
            //1.4 根据学生的回答进行扣分
            if (! q.getRightOption().str.equals(answer)){
                //答错一题扣20分
                grade -= 20;
            }
        }
        //2、答题结束后打印最终得分
        System.out.println("此次考试的得分为：" + grade);
        //3 记录到学生信息中
        if (null == student.getScores()){
            List<Double> grades = new ArrayList<>();
            grades.add(grade);
            student.setScores(grades);
        }else{
            student.getScores().add(grade);
        }
    }

    private void exportGrades() throws IOException {
        //1、根据学生输入的路径进行分数的导出
        System.out.println("请输入分数导出的存储路径：");
        String path = ClientScanner.getScanner().next();
        FileWriter fs = new FileWriter(path + "/" +
                LocalDate.now() + ".txt");
        for (double s : student.getScores()){
            fs.write(Double.toString(s));
            fs.write("\n");
        }
        System.out.println("导出成绩成功！");
        fs.close();
    }

    private void searchGrades(){
        //1 在屏幕打印所有考试的分数
        System.out.println("历史得分：");
        for (double s : student.getScores()){
            System.out.println(s);
        }
    }
}
